#include "graphic_shader_program.h"
/*--------------------------------------------------------------------------------*/
PFNGLCREATEPROGRAMPROC
	GRAPHIC_SHADER_PROGRAM::glCreateProgram = NULL;
PFNGLDELETEPROGRAMPROC
	GRAPHIC_SHADER_PROGRAM::glDeleteProgram = NULL;
PFNGLATTACHSHADERPROC
	GRAPHIC_SHADER_PROGRAM::glAttachShader = NULL;
PFNGLDETACHSHADERPROC
	GRAPHIC_SHADER_PROGRAM::glDetachShader = NULL;
PFNGLLINKPROGRAMPROC
	GRAPHIC_SHADER_PROGRAM::glLinkProgram = NULL;
PFNGLGETPROGRAMIVPROC
	GRAPHIC_SHADER_PROGRAM::glGetProgramiv = NULL;
PFNGLGETPROGRAMINFOLOGPROC
	GRAPHIC_SHADER_PROGRAM::glGetProgramInfoLog = NULL;
PFNGLUSEPROGRAMPROC
	GRAPHIC_SHADER_PROGRAM::glUseProgram = NULL;
/*--------------------------------------------------------------------------------*/
VOID GRAPHIC_SHADER_PROGRAM::Initialize( const GRAPHIC_SHADER vertex_shader,  const GRAPHIC_SHADER pixel_shader  )
{
	DEBUG_Require( !Id );
	DEBUG_Require( vertex_shader.GetType() == GRAPHIC_SHADER::TYPE_VertexShader );
	DEBUG_Require( pixel_shader.GetType() == GRAPHIC_SHADER::TYPE_PixelShader );

	Id = glCreateProgram();

	AddShader( vertex_shader );
	AddShader( pixel_shader );

	glLinkProgram( Id );
	DEBUG_CheckOpenGL();

	if( !IsLinked() )
	{
		DEBUG_Log( "Error while linking shader program :\n" );
		DEBUG_Log( GetInfoLog() );
	}
	else
	{

	}
}
/*--------------------------------------------------------------------------------*/
VOID GRAPHIC_SHADER_PROGRAM::AddShader( const GRAPHIC_SHADER shader )
{
	glAttachShader( Id, shader.GetId() );
}
/*--------------------------------------------------------------------------------*/
VOID GRAPHIC_SHADER_PROGRAM::Enable()
{
	glUseProgram( Id );
}
/*--------------------------------------------------------------------------------*/
VOID GRAPHIC_SHADER_PROGRAM::Disable()
{
	glUseProgram( 0 );
}
/*--------------------------------------------------------------------------------*/
BOOL GRAPHIC_SHADER_PROGRAM::IsLinked() const
{
	GLint
		link_status = GL_TRUE;

	glGetProgramiv( Id, GL_LINK_STATUS, &link_status);

	if( link_status != GL_TRUE )
	{
		return false;
	}

	return true;
}
/*--------------------------------------------------------------------------------*/
const TEXT GRAPHIC_SHADER_PROGRAM::GetInfoLog() const
{
	GLint
		log_size;
	CHAR
		buffer[ 1024 ];

	glGetProgramiv( Id, GL_INFO_LOG_LENGTH, &log_size );

	glGetProgramInfoLog( Id, 1024, &log_size, buffer);

	return TEXT( buffer );
}
/*--------------------------------------------------------------------------------*/

